package com.cuiweiyou.uiautomator20;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * www.gaohaiyan.com
 */
public class ManagerWord {
    /**
     * 没有匹配到对方话术时的随意回复
     */
    private List<String> emptyAnswerMsgList;
    /**
     * 匹配到回复话术时的随意的前缀词汇
     */
    private List<String> prefixAnswerMsgList;
    /**
     * 分词，话术中有意义的固定的词汇
     */
    private List<String> fixedWordList;
    /**
     * 分词，话术中无关紧要的词汇
     */
    private List<String> delWordList;
    /**
     * 近义词
     */
    private Map<String, List<String>> homoionymWordMap;

    /**
     * 初次问候
     */
    private List<String> helloWordList;

    private static ManagerWord instance;

    private ManagerWord() {
        homoionymWordMap = new HashMap<>();

        delWordList = new ArrayList<>();
        helloWordList = new ArrayList<>();
        fixedWordList = new ArrayList<>();
        emptyAnswerMsgList = new ArrayList<>();
        prefixAnswerMsgList = new ArrayList<>();
    }

    public static ManagerWord getInstance() {
        if (null == instance) {
            synchronized (ManagerWord.class) {
                if (null == instance) {
                    instance = new ManagerWord();
                }
            }
        }
        return instance;
    }

    /**
     * 是否是打招呼语句<br/>
     *
     * @param word
     * @return
     */
    public boolean isHelloWord(String word) {
        if (helloWordList.size() < 1) {
            return false;
        }
        boolean contains = helloWordList.contains(word);
        if (!contains) {
            for (int i = 0; i < helloWordList.size(); i++) {
                String str = helloWordList.get(i);
                if (word.contains(str)) {
                    contains = true;
                    break;
                }
            }
        }
        UtilLogs.saveLog(word + "，是否为打招呼：" + contains);
        return contains;
    }

    /**
     * 检查意义特殊的固定词汇
     *
     * @param string
     * @return
     */
    public List<String> getFixedWord(String string) {
        if (null == string || string.length() < 1) {
            return null;
        }

        List<String> result = new ArrayList<>();
        for (int i = 0; i < fixedWordList.size(); i++) {
            String word = fixedWordList.get(i);
            if (string.contains(word)) {
                if (!result.contains(word)) {
                    result.add(word);
                }
            }
        }
        UtilLogs.saveLog(string + "：固定词：" + result);
        return result;
    }

    /**
     * 取近义词
     *
     * @param word 分词后得出的词语
     * @return 近义词集合，可能为null
     */
    public List<String> getHomoionymWord(String word) {
        if (homoionymWordMap.size() < 1) {
            return new ArrayList<String>();
        }
        List<String> strings = homoionymWordMap.get(word);
        if (null == strings) {
            return new ArrayList<String>();
        }
        UtilLogs.saveLog(word + "，近义词：" + strings.toString());
        return strings;
    }

    /**
     * 没有匹配到对方话术时的随意回复
     *
     * @return
     */
    public String getRandomEmptyAnswerMsg() {
        int size = emptyAnswerMsgList.size();
        if (0 == size) {
            return "";
        }
        int index = new Random().nextInt(size - 1);
        return emptyAnswerMsgList.get(index);
    }

    /**
     * 回复话术时的随意的前缀词汇
     *
     * @return
     */
    public String getPrefixAnswerMsg() {
        int size = prefixAnswerMsgList.size();
        if (0 == size) {
            return "";
        }
        int index = new Random().nextInt(size);
        return prefixAnswerMsgList.get(index);
    }

    /**
     * 去除对方语句中无意义词汇
     *
     * @param string
     * @return
     */
    public String delNormalOtherWord(String string) {
        UtilLogs.saveLog("原始句子：" + string);
        String result = string;
        for (int i = 0; i < delWordList.size(); i++) {
            String word = delWordList.get(i);
            result = result.replace(word, "");
        }
        UtilLogs.saveLog("过滤词后句子：" + result);
        return result;
    }

    public void init() {
        File wordFile = UtilFile.getWordFile();
        if (!wordFile.exists()) {
            UtilLogs.saveLog("没有word分词文件");
            return;
        }

        try {
            InputStream is = new FileInputStream(wordFile);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            List<String> group = new ArrayList<>();
            while (null != (line = reader.readLine())) {
                line = line.trim();
                if (line.length() < 1) {
                    continue;
                }
                if (line.startsWith("###")) { // 新的一句
                    saveGimmick(group);
                }
                group.add(line);
            }

            saveGimmick(group);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void saveGimmick(List<String> group) {
        if (group.size() > 0) {
            String line = group.get(0);
            if (line.startsWith("###")) { // 分组标记
                line = line.replace("###", "");
                if (line.startsWith("EA")) {                // 组名 EA.随意的无意义的回复
                    initWordGroup("ea", emptyAnswerMsgList, group);
                } else if (line.startsWith("PREFIX")) {     // 组名 PREFIX.随意的无意义的回复前缀
                    initWordGroup("prefix", prefixAnswerMsgList, group);
                } else if (line.startsWith("HOMOIONYM")) {  // 组名 HOMOIONYM.近义词
                    initHomoionymWord(group);
                } else if (line.startsWith("HELLO")) {      // 组名 HELLO.加好友后对方的问话，如果出现都视为初次问好
                    initWordGroup("hello", helloWordList, group);
                } else if (line.startsWith("FIX")) {        // 组名 FIX.固定短语保留
                    initWordGroup("fix", fixedWordList, group);
                } else if (line.startsWith("DEL")) {        // 组名 DEL.这些词从对方的话中过滤掉，去掉
                    initWordGroup("del", delWordList, group);
                }
            }
        }
        group.clear();
    }

    private void initWordGroup(String title, List<String> target, List<String> group) {
        for (int i = 1; i < group.size(); i++) { // index为1，忽略组名一行
            String word = group.get(i);
            target.add(word);
        }
    }

    private void initHomoionymWord(List<String> group) {
        List<String> array = new ArrayList<>();
        for (int i = 0; i < group.size(); i++) {
            String line = group.get(i).trim();
            if (line.length() < 1) {
                continue;
            }
            if (line.startsWith("$$$ARR")) {        // 近义词分组
                saveHmoionymArray(array);
            } else if (line.startsWith("***")) {    // 注释
                continue;
            }

            array.add(line);

        }
        saveHmoionymArray(array);
    }

    private void saveHmoionymArray(List<String> array) {
        if (array.size() > 0) {
            List<String> arrayList = new ArrayList<>();
            arrayList.addAll(array);

            String groupName = arrayList.get(0).replace("$$$ARR", "");

            for (int i = 1; i < arrayList.size(); i++) { // index=1，忽略小组名
                String word = arrayList.get(i);

                UtilLogs.saveLog("近义词分组 " + groupName + "，有：" + word);

                homoionymWordMap.put(word, arrayList);
            }
        }

        array.clear();
    }
}
